package com.lc.communication.listener;


import com.lc.communication.service.HeartBeatClient;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;

import java.util.concurrent.TimeUnit;

public class ConnectionListener implements ChannelFutureListener {

    private String uuid;

    private String clientName;


    private String ip;

    private int port;


    public ConnectionListener(String clientName,String uuid, String ip, int port) {
        super();
        this.clientName = clientName;
        this.uuid = uuid;
        this.ip = ip;
        this.port = port;
    }

    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        if (!channelFuture.isSuccess()) {
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule(() -> {
                System.err.println("服务端链接不上，开始重连操作...");
                //ChannelMap.CHANNEL_MAP.remove(name);
                HeartBeatClient.Connection.connect(clientName,uuid, ip, port);
            }, 1L, TimeUnit.SECONDS);
        } else {
            System.err.println("服务端断开...");
        }
    }

}

